module.exports = {
  /**
	 * 用户模块
	 * 权限标识：
	 *  - READ_USER
	 *  - ADD_USERE
	 *  - DELETE_USER
	 *  - UPDATE_USER
	 * 

  /**
   * 获取用户
   */
  async getUsers() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)

    const skip = params.pageNum == 1 ? 0 : params.pageSize * (params.pageNum - 1)
    let res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-users')
      .field('_id,username,nickname,role,register_date,last_login_date,last_login_ip')
      .skip(skip)
      .limit(params.pageSize)
      .get({
        getCount: true
      })

    return res
  },

  /**
   * 删除用户
   */
  async deleteUser() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    const authInfo = this.authInfo || {}

    let res = { errCode: 403, errMsg: '无权限访问' }
    if (Array.isArray(authInfo.role) && authInfo.role.includes('admin')) {
      res = await uniCloud
        .databaseForJQL({ clientInfo })
        .collection('uni-id-users')
        .where({ _id: params.uid })
        .remove()
    }

    return res
  },

  /**
   * 检查用户名是否可用
   */
  async checkUserName() {
    const { body = {} } = this.getHttpInfo()
    const { params, clientInfo } = JSON.parse(body)
    // const authInfo = this.authInfo || {}

    let res = await uniCloud
      .databaseForJQL({ clientInfo })
      .collection('uni-id-users')
      .where({ username: params.username })
      .count()

    if (res.total != 0) {
      res = { errCode: 400, errMsg: '用户名太受欢迎了，换一个吧！' }
    }

    return res
  }
}
